package zhupf.test.juc;

import java.util.concurrent.*;

/**
 * 线程池
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
//        ExecutorService executorService = Executors.newFixedThreadPool(5);//一池五个固定的线程
//        ExecutorService executorService = Executors.newSingleThreadExecutor();//一池一个线程
//        ExecutorService executorService = Executors.newCachedThreadPool();//一池N个线程
        System.out.println(Runtime.getRuntime().availableProcessors());
        ExecutorService executorService = new ThreadPoolExecutor(2,
                Runtime.getRuntime().availableProcessors()+1,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());
        try {
            for (int i = 1; i <= 10; i++) {

                executorService.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "\t 线程执行");
                });
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executorService.shutdown();
        }
    }
}
